package com.san.app.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.app.domain.WarningDTO;
import com.san.app.service.WarningReviewService;
import com.san.app.vo.WarningSubVO;
import com.san.base.controller.BaseController;
import com.san.common.annotation.Log;
import com.san.common.utils.Constant;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;
import com.san.oa.service.NotifyService;
import com.san.oa.vo.NotifyVO;

/**
 * 预警审核控制层
 * @author SuperSR
 * @date 2019年3月19日 下午5:23:35
 *
 */
@RequestMapping("/app/warningreview")
@Controller
public class WarningReviewController extends BaseController{

	@Autowired
	private WarningReviewService warningReviewService;
	@Autowired
	private NotifyService notifyService;
	
	private static final Logger logger = LoggerFactory.getLogger(WarningReviewController.class);
	
	private String PREFIX = "app/warningreview";
	
	@GetMapping("")
	@RequiresPermissions("app:warningreview:view")
	String source(Model model) {
		return PREFIX + "/view";
	}

	/**
	 * 进入预警审核列表
	 * @param params
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("app:warningreview:view")
	@ResponseBody
	public PageUtils list(@RequestParam Map<String, Object> params) {
		PageUtils pageUtils = null;
		List<WarningDTO> list = null;
		try {
			Query query = new Query(params);
			list = warningReviewService.reviewList(query);
			int total = warningReviewService.count(query);
			pageUtils = new PageUtils(list, total);
		} catch (Exception e) {
			logger.error("获取审核信息数据出错",e);
		}
		return pageUtils;
	}
	
	/**
	 * 审核界面
	 * @param model
	 * @param id
	 * @param modelName
	 * @param operator
	 * @return
	 */
	@GetMapping("/review/{id}/{modelName}/{operator}")
	@RequiresPermissions("app:warningreview:review")
	public String review(Model model, @PathVariable("id") Long id,@PathVariable("modelName") String modelName,@PathVariable("operator") Long operator) {
		model.addAttribute("id", id);
		model.addAttribute("modelName",modelName);
		model.addAttribute("operator",operator);
		return PREFIX + "/approve";
	}

	/**
	 * 预警审核
	 * @param id
	 * @param reason
	 * @param modelName
	 * @param operator
	 * @param state
	 * @return
	 */
	@PostMapping("/approve")
	@RequiresPermissions("app:warningreview:review")
	@ResponseBody
	@Log("预警审核")
	public R approve(Long id,String reason,String modelName,Long operator,Integer state){
		try {
			WarningSubVO warningSubVO = new WarningSubVO();
			warningSubVO.setId(id);
			warningSubVO.setState(state);
			warningSubVO.setReason(reason);
			warningReviewService.review(warningSubVO);
			//给审核对象发送审核成功信息
			NotifyVO notify = new NotifyVO();
			String content = "";
			if(state == 1){
				notify.setTitle("预警订阅审核通知");
				notify.setCreateUserId(getUserId());
				if(reason != null && !"".equals(reason)){
					content = modelName + "订阅审核通过\n原因：" + reason;
				}else {
					content = modelName + "订阅审核通过";
				}
				notify.setContent(content);
				notify.setUserIds(new Long[]{operator});
				notify.setCreateDate(new Date());
				notify.setStatus(Constant.OA_NOTIFY_STATUS_PUBLISH);
				notify.setType(Constant.OA_NOTIFY_TYPE_SYSTEM);
				notify.setRemarks("系统通知");
			} else if(state == 2) {
				notify.setTitle("预警订阅审核通知");
				notify.setCreateUserId(getUserId());
				notify.setContent(modelName+"订阅审核未通过\n原因："+reason);
				notify.setUserIds(new Long[]{operator});
				notify.setCreateDate(new Date());
				notify.setStatus(Constant.OA_NOTIFY_STATUS_PUBLISH);
				notify.setType(Constant.OA_NOTIFY_TYPE_SYSTEM);
				notify.setRemarks("系统通知");
			}
			notifyService.save(notify);
			return R.ok("操作成功");
		} catch (Exception e) {
			logger.error("操作失败");
		}
		return R.ok("操作失败");
	}
	
}













